<?php
/*
+-----------------------------------------------------------------------------+
| $Id: sys.class.php 2009-08-18 08:32:25Z Bleakwind $
| System class
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
| Release under the GPL licenses:
|   http://www.gnu.org/licenses/gpl.html
+-----------------------------------------------------------------------------+
*/

class sys
{
    var $ip                     = "";

    var $get                    = array();
    var $post                   = array();

    var $magic_quotes_gpc       = 0;
    var $nowtime;

    var $channel_all            = array();
    var $channel_upper          = array();
    var $channel_current        = array();
    var $channel_sub            = array();
    var $channel_allsub         = array();
    var $channel_tree           = array();

    var $area_array             = array();
    var $area_select            = array();

    // sys
    function sys()
    {
        // Return System Configuration
        $this->magic_quotes_gpc = get_magic_quotes_gpc();
        $this->nowtime          = time();

        // Return $_GET and $_POST
        $this->return_get();
        $this->return_post();

        // Return My IP
        $this->return_ip();

        return;
    }

    /*
    +-----------------------------------------------------------------------------+
    | public
    +-----------------------------------------------------------------------------+
    */
    // stand_url
    function stand_url()
    {
        global $PAGE_PATH;
        $page_http  = $_SERVER['HTTPS'] == "on" ? "https://" : "http://";
        $result     = parse_url(CONFIG_ROOTDIR);
        $account    = $result['user'] != "" ? $result['user'].":".$result['pass']."@" : "";
        $host       = $result['host'];
        $port       = $result['port'] != "" ? ":".$result['port'] : "";
        $url        = $result['scheme']."://".$account.$host.$port.$_SERVER['REQUEST_URI'];
        if ($host.$port != $_SERVER['HTTP_HOST'] || $result['scheme']."://" != $page_http) {
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: ".$url);
            exit;
        }
        return;
    }

    // return_get
    function return_get()
    {
        $return_get = array();
        if ( is_array($_GET) ) {
            foreach ($_GET as $k => $v) {
                if ( is_array($_GET[$k]) ) {
                    while( list($k2, $v2) = each($_GET[$k]) ) {
                        $return_get[$k][$k2] = $this->clean_value($v2,true);
                    }
                } else {
                    $return_get[$k] = $this->clean_value($v,true);
                }
            }
        }
        $this->get = $return_get;

        return $this->get;
    }

    // return_post
    function return_post()
    {
        $return_post = array();
        if ( is_array($_POST) ) {
            foreach ($_POST as $k => $v) {
                if ( is_array($_POST[$k]) ) {
                    while( list($k2, $v2) = each($_POST[$k]) ) {
                        $return_post[$k][ $k2] = $this->clean_value($v2);
                    }
                } else {
                    $return_post[$k] = $this->clean_value($v);
                }
            }
        }
        $this->post = $return_post;

        return $this->post;
    }

    // clean_value
    function clean_value($val, $strip=false)
    {
        if ( $this->magic_quotes_gpc ) {
            $val = stripslashes($val);
        }
        if ($strip === true) {
            $val = strip_tags($val);
            $val = strtolower($val);
            $val = trim($val);
        }

        return $val;
    }

    // clean_variable
    function clean_variable()
    {
        return;
    }

    // check_login
    function check_login($cookie, $message="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $db, $SYSCHECK, $LANGUAGE, $MEMBER;
        $returnurl      = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $login_warning  = $LANGUAGE['s']['_public']['source_lib_sys_class']['login_warning']."<!-- ".$this->get['act']." error for login -->";
        if (in_array($this->get['act'], $SYSCHECK['list_login_member'])) {
            if (!$cookie->iflogin()) {
                $this->prompt("jump", $CONFIGURE['common']['control_index']."?act=login&msg=".htmlentities(urlencode($login_warning))."&ret=".htmlentities(urlencode($returnurl)));
                exit;
            }
        }
        return;
    }

    // check_privilege
    function check_privilege($message="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $db, $SYSCHECK, $LANGUAGE, $MEMBER;
        $returnurl          = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $privileges_warning = $LANGUAGE['s']['_public']['source_lib_sys_class']['privileges_warning']."<!-- ".$this->get['act']." error for privilege -->";
        if (in_array($this->get['act'], $SYSCHECK['list_privilege_member'])) {
            if ($MEMBER['mg_privilege'][$this->get['act']] != "1") {
                $this->prompt("failed", $privileges_warning, $returnurl);
            }
        }
        return;
    }

    // check_fileallow
    function check_fileallow($message="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $db, $SYSCHECK, $LANGUAGE, $MEMBER;
        $returnurl          = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $fileallow_warning  = $LANGUAGE['s']['_public']['source_lib_sys_class']['fileallow_warning']."<!-- ".$this->get['act']." error for fileallow -->";
        if (array_key_exists($this->get['act'], $SYSCHECK['list_fileallow_member'])) {
            if ($MEMBER['mg_fileallow'][$this->get['act']] != "1") {
                $this->prompt("failed", $fileallow_warning, $returnurl);
            }
        }
        return;
    }

    // set_prompt
    function set_prompt($varname="")
    {
        global $db, $prompt_id;
        $str_query = "";
        if (is_array($varname)) {
            foreach ($varname as $k => $v) {
                $str_query .= $k."=".$v.",";
            }
            $str_query = substr($str_query, 0, -1);
        } else {
            $str_query = "prompt_type=null,prompt_if=0,prompt_message=null,prompt_returntext=null,prompt_returnurl=null,prompt_image=null,prompt_act=''";
        }
        $sql = "UPDATE ".DB_TABLE_PROMPT."
                SET ".$str_query."
                WHERE prompt_id='".$prompt_id."'";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
            $return = FALSE;
        } else {
            $return = TRUE;
        }
        return $return;
    }

    // Prompt message
    // jump : function prompt($status="", $url="",     $message="",   $returnurl="",  $image="")
    // other: function prompt($status="", $message="", $returnurl="", $returntext="", $image="")
    function prompt($status="", $message="", $returnurl="", $returntext="", $image="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $db, $LANGUAGE;
        if ($status == "jump") {
            $url       = $message;
            $message   = $returnurl;
            $returnurl = $returntext;
            $this->set_prompt(array(
                'prompt_type'       => "'jump'",
                'prompt_if'         => "1",
                'prompt_message'    => "'".$message."'",
                'prompt_returnurl'  => "'".$returnurl."'",
                'prompt_act'        => "'".$this->get['act']."'",
            ));
            header("location:".$url);
            exit;
            return;
        } elseif ($status == "succeed") {
            $message    = $message    ? $message    : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_succeed'];
            $returnurl  = $returnurl  ? $returnurl  : "javascript:history.back(1)";
            $returntext = $returntext ? $returntext : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_return'];
            $image      = $image      ? $image      : "succeed.gif";
        } elseif ($status == "failed") {
            $message    = $message    ? $message    : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_failed'];
            $returnurl  = $returnurl  ? $returnurl  : "javascript:history.back(1)";
            $returntext = $returntext ? $returntext : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_return'];
            $image      = $image      ? $image      : "failed.gif";
        } else {
            $message    = $message    ? $message    : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_unallowed'];
            $returnurl  = $returnurl  ? $returnurl  : "javascript:history.back(1)";
            $returntext = $returntext ? $returntext : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_return'];
            $image      = $image      ? $image      : "warning.gif";
        }
        $this->set_prompt(array(
            'prompt_type'       => "'prompt'",
            'prompt_if'         => "1",
            'prompt_message'    => "'".$message."'",
            'prompt_returnurl'  => "'".$returnurl."'",
            'prompt_returntext' => "'".$returntext."'",
            'prompt_image'      => "'".$image."'",
            'prompt_act'        => "'prompt'",
        ));
        @header("location:".$CONFIGURE['common']['control_index']."?act=prompt");
        exit;
        return;
    }

    // Prompt message for login member
    // jump : function prompt($status="", $url="",     $message="",   $returnurl="",  $image="")
    // other: function prompt($status="", $message="", $returnurl="", $returntext="", $image="")
    function cp_prompt($status="", $message="", $returnurl="", $returntext="", $image="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $db, $LANGUAGE;
        if ($status == "jump") {
            $url       = $message;
            $message   = $returnurl;
            $returnurl = $returntext;
            $this->set_prompt(array(
                'prompt_type'       => "'jump'",
                'prompt_if'         => "1",
                'prompt_message'    => "'".$message."'",
                'prompt_returnurl'  => "'".$returnurl."'",
                'prompt_act'        => "'".$this->get['act']."'",
            ));
            header("location:".$url);
            exit;
            return;
        } elseif ($status == "succeed") {
            $message    = $message    ? $message    : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_succeed'];
            $returnurl  = $returnurl  ? $returnurl  : "javascript:history.back(1)";
            $returntext = $returntext ? $returntext : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_return'];
            $image      = $image      ? $image      : "succeed.gif";
        } elseif ($status == "failed") {
            $message    = $message    ? $message    : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_failed'];
            $returnurl  = $returnurl  ? $returnurl  : "javascript:history.back(1)";
            $returntext = $returntext ? $returntext : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_return'];
            $image      = $image      ? $image      : "failed.gif";
        } else {
            $message    = $message    ? $message    : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_unallowed'];
            $returnurl  = $returnurl  ? $returnurl  : "javascript:history.back(1)";
            $returntext = $returntext ? $returntext : $LANGUAGE['s']['_public']['source_lib_sys_class']['prompt_return'];
            $image      = $image      ? $image      : "warning.gif";
        }
        $this->set_prompt(array(
            'prompt_type'       => "'cp_prompt'",
            'prompt_if'         => "1",
            'prompt_message'    => "'".$message."'",
            'prompt_returnurl'  => "'".$returnurl."'",
            'prompt_returntext' => "'".$returntext."'",
            'prompt_image'      => "'".$image."'",
            'prompt_act'        => "'cp_prompt'",
        ));
        @header("location:".$CONFIGURE['common']['control_index']."?act=cp_prompt");
        exit;
        return;
    }

    // Return visitor's IP
    function return_ip()
    {
        if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif ($_SERVER['HTTP_CLIENT_IP']) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif ($_SERVER['REMOTE_ADDR']) {
            $ip = $_SERVER['REMOTE_ADDR'];
        } else {
            $ip = "Unknown";
        }
        $this->ip = trim($ip);

        return $this->ip;
    }

    // forbid_ip
    function forbid_ip()
    {
        global $CONFIG, $CONFIGURE, $SETTING, $db, $LANGUAGE, $MEMBER, $t;
        $if_isset_ip = "2";
        if ($this->ip == "") { $this->return_ip(); }
        $ip_part = explode(".", $this->ip);
        $sql = "SELECT *
                FROM ".DB_TABLE_FORBID_IP."
                WHERE (ip1='".$ip_part[0]."' OR ip1='*')
                        AND (ip2='".$ip_part[1]."' OR ip2='*')
                        AND (ip3='".$ip_part[2]."' OR ip3='*')
                        AND (ip4='".$ip_part[3]."' OR ip4='*')
                        AND ((time_begin < ".$this->nowtime." OR time_begin = 0) AND (time_end > ".$this->nowtime." OR time_end = 0) )";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if ($result->RecordCount() > 0) {
                /*echo "<div align='center'><div style='width:300px;padding: 3px;background-color:#D6DFF7;border:#1151BF 1px solid;font-family:sans-serif;font-size: 11px;'>\n";
                echo "<b>".$LANGUAGE['s']['_public']['source_lib_sys_class']['forbid_ip_message']."</b>\n";
                echo "</div></div>\n";*/
                //exit;
                $if_isset_ip = "1";
            }
        }
        return $if_isset_ip;
    }

    // return_select
    function return_select($sort)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $select_option = array();
        if (is_array($sort)) {
            $array_select   = $sort;
            $list_select    = implode("','", $array_select);
        } else {
            $array_select   = explode(",", $sort);
            $list_select    = implode("','", $array_select);
        }
        if (!empty($array_select)) {
            $sql = "SELECT id,sort,name,value,if_default
                    FROM ".DB_TABLE_SELECT."
                    WHERE sort IN ('".$list_select."') ORDER BY rank,id";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
            } else {
                while (!$result->EOF) {
                    if (in_array($result->fields['sort'], $array_select)) {
                        $select_option{$result->fields['sort']}[$result->fields['id']] = array(
                            'id'         => $result->fields['id'],
                            'sort'       => $result->fields['sort'],
                            'name'       => $result->fields['name'],
                            'value'      => $result->fields['value'],
                            'if_default' => $result->fields['if_default'],
                        );
                    }
                    $result->MoveNext();
                }
            }
        }
        return $select_option;
    }

    // return_select_value
    function return_select_value($id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $return = array();
        if (!empty($id)) {
            $sql = "SELECT name,value
                    FROM ".DB_TABLE_SELECT."
                    WHERE id=".$id."";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
            } else {
                if (!$result->EOF) {
                    $return['name']     = $result->fields['name'];
                    $return['value']    = $result->fields['value'];
                }
            }
        }
        return $return;
    }

    /*
    +-----------------------------------------------------------------------------+
    | private
    +-----------------------------------------------------------------------------+
    */

    // auto_run
    function auto_run()
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        // ...
    }

    // About Article
    // return_article_info
    function return_article_info($article_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $sql = "SELECT article.*,
                    detail.detail as detail, detail.page_type as detail_page_type, detail.page_word as detail_page_word,
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_ARTICLE." article
                    LEFT JOIN ".DB_TABLE_ARTICLE_DETAIL." detail ON detail.article_id=article.id
                    LEFT JOIN ".DB_TABLE_ARTICLE_COVER." cover ON cover.article_id=article.id
                WHERE article.id=".$article_id."";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if (!$result->EOF) {
                $article_info = array(
                        'id'                        => $result->fields['id'],
                        'phrase'                    => $result->fields['phrase'],
                        'phrase_len'                => mb_strwidth($result->fields['phrase'],"UTF-8"),
                        'subject'                   => $result->fields['subject'],
                        'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                        'prefix'                    => $result->fields['prefix'],
                        'color'                     => $result->fields['color'],
                        'fontstyle'                 => $result->fields['fontstyle'],
                        'decoration'                => $result->fields['decoration'],
                        'keyword'                   => $result->fields['keyword'],
                        'brief'                     => $result->fields['brief'],
                        'post_id'                   => $result->fields['post_id'],
                        'post_username'             => $result->fields['post_username'],
                        'post_ip'                   => $result->fields['post_ip'],
                        'post_time'                 => $result->fields['post_time'],
                        'amend_id'                  => $result->fields['amend_id'],
                        'amend_username'            => $result->fields['amend_username'],
                        'amend_ip'                  => $result->fields['amend_ip'],
                        'amend_time'                => $result->fields['amend_time'],
                        'click'                     => $result->fields['click'],
                        'if_enable'                 => $result->fields['if_enable'],
                        'if_enable_begin'           => $result->fields['if_enable_begin'],
                        'if_enable_end'             => $result->fields['if_enable_end'],
                        'if_top'                    => $result->fields['if_top'],
                        'if_top_begin'              => $result->fields['if_top_begin'],
                        'if_top_end'                => $result->fields['if_top_end'],
                        'if_turn'                   => $result->fields['if_turn'],
                        'if_turn_type'              => $result->fields['if_turn_type'],
                        'if_turn_url'               => $result->fields['if_turn_url'],

                        'detail'                    => $result->fields['detail'],
                        'page_type'                 => $result->fields['page_type'],
                        'page_word'                 => $result->fields['page_word'],

                        'cover_id'                  => $result->fields['cover_id'],
                        'cover_dir'                 => $result->fields['cover_dir'],
                        'cover_filename'            => $result->fields['cover_filename'],
                );

                $article_info['detail_list']    = explode("[PAGESPLIT]", $article_info['detail']);
                $article_info['detail_num']     = count($article_info['detail_list']);

                $article_info['attach']         = func::db_select(DB_TABLE_ARTICLE_ATTACH, "*", "article_id=".$result->fields['id'], "", "id");
            }
        }
        return $article_info;
    }

    // return_article_list
    function return_article_list($limit="", $condition="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;

        $sql_where      = "";
        $sql_groupby    = "";
        $sql_orderby    = "";
        $sql_limit      = "";

        $article_list   = array();

        if (!empty($condition['article_id'])){
            if (!is_array($condition['article_id'])) {
                $condition['article_id'] = explode(",", $condition['article_id']);
            }
            $condition['article_id'] = func::array_clean($condition['article_id']);
            if (is_array($condition['article_id']) && count($condition['article_id']) > 0){
                if (count($condition['article_id']) > 1){
                    $sql_where .= " AND article.id IN (".implode(",", $condition['article_id']).")";
                } else {
                    $sql_where .= " AND article.id=".implode(",", $condition['article_id'])."";
                }
            }
        }

        if (!empty($condition['except_article_id'])){
            if (!is_array($condition['except_article_id'])) {
                $condition['except_article_id'] = explode(",", $condition['except_article_id']);
            }
            $condition['except_article_id'] = func::array_clean($condition['except_article_id']);
            if (is_array($condition['except_article_id']) && count($condition['except_article_id']) > 0){
                if (count($condition['except_article_id']) > 1){
                    $sql_where .= " AND article.id NOT IN (".implode(",", $condition['except_article_id']).")";
                } else {
                    $sql_where .= " AND article.id!=".implode(",", $condition['except_article_id'])."";
                }
            }
        }

        if ($condition['if_top'] == "1") {
            $sql_where .= " AND ((article.if_top=1) || (article.if_top=3 AND article.if_top_begin<".time()." AND article.if_top_begin>".time()."))";
        }

        if ($condition['cover'] == "1") {
            $sql_where .= " AND cover.filename!=''";
        }

        $sql_where = substr(trim($sql_where), 0, 3) == "AND" ? substr(trim($sql_where), 3) : trim($sql_where);
        $sql_where = $sql_where != "" ? "WHERE ".$sql_where : "";

        if ($condition['click'] == "1"){
            $sql_orderby .= " click DESC,";
        }

        if ($condition['rand'] == "1"){
            $sql_orderby .= " rand(),";
        }

        if ((int)$limit > 0){
            $sql_limit .= " LIMIT ".(int)$limit;
        } else {
            $sql_limit .= " LIMIT ".$CONFIG['page_record_article_list'];
        }

        $sql = "SELECT article.*,
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_ARTICLE." article
                    LEFT JOIN ".DB_TABLE_ARTICLE_COVER." cover ON cover.article_id=article.id
                ".$sql_where."
                ORDER BY ".$sql_orderby." article.if_top, article.post_time DESC ".$sql_limit;
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $article_list[] = array(
                    'id'                        => $result->fields['id'],
                    'phrase'                    => $result->fields['phrase'],
                    'phrase_len'                => mb_strwidth($result->fields['phrase'],"UTF-8"),
                    'subject'                   => $result->fields['subject'],
                    'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                    'prefix'                    => $result->fields['prefix'],
                    'color'                     => $result->fields['color'],
                    'fontstyle'                 => $result->fields['fontstyle'],
                    'decoration'                => $result->fields['decoration'],
                    'keyword'                   => $result->fields['keyword'],
                    'brief'                     => $result->fields['brief'],
                    'post_id'                   => $result->fields['post_id'],
                    'post_username'             => $result->fields['post_username'],
                    'post_ip'                   => $result->fields['post_ip'],
                    'post_time'                 => $result->fields['post_time'],
                    'amend_id'                  => $result->fields['amend_id'],
                    'amend_username'            => $result->fields['amend_username'],
                    'amend_ip'                  => $result->fields['amend_ip'],
                    'amend_time'                => $result->fields['amend_time'],
                    'click'                     => $result->fields['click'],
                    'if_enable'                 => $result->fields['if_enable'],
                    'if_enable_begin'           => $result->fields['if_enable_begin'],
                    'if_enable_end'             => $result->fields['if_enable_end'],
                    'if_top'                    => $result->fields['if_top'],
                    'if_top_begin'              => $result->fields['if_top_begin'],
                    'if_top_end'                => $result->fields['if_top_end'],
                    'if_turn'                   => $result->fields['if_turn'],
                    'if_turn_type'              => $result->fields['if_turn_type'],
                    'if_turn_url'               => $result->fields['if_turn_url'],

                    'cover_id'                  => $result->fields['cover_id'],
                    'cover_dir'                 => $result->fields['cover_dir'],
                    'cover_filename'            => $result->fields['cover_filename'],
                );
                $result->MoveNext();
            }
        }
        return $article_list;
    }

    // return_article_attach_list
    function return_article_attach_list($attach_list, $attach_type, $attach_where, $attach_table, $attach_dir)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $str_js_han     = "";
        $result_str     = "";
        $result_js      = "";
        $filelist       = func::db_select($attach_table, "*", $attach_where, "", "id DESC");
        if (is_array($filelist) && count($filelist) > 0){
            $result_str = "";
            $result_str .= "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\">";
            foreach($filelist as $file){
                $path   = $attach_dir."/".$file['dir'];
                if (file_exists($path.$file['filename'])) {
                    list($width, $height, $type, $attr) = @getimagesize($path.$file['filename']);
                }
                if (preg_match("/^(\.gif|\.jpg|\.png)$/i", substr($file['filename'], -4))){
                    $file_icon = "<img src=\"".CONFIG_ROOTDIR.$path.$file['filename']."\" width=\"45\" height=\"45\" border=\"0\" align=\"absmiddle\">";
                } else {
                    $file_icon = "&nbsp;";
                }
                if ($attach_type == "add") { $file['article_id'] = $MEMBER['id']; }
                $result_str .= "
                  <tr align=\"center\" bgcolor=\"#DEDEDE\">
                    <td width=\"15%\">
                      <span title=\"删除附件\"><img src=\"".$SETTING['dir_images_admin']."/icon_delete.gif\" onclick=\"xajax_attach_del('".$attach_type."','".$file['article_id']."','".$file['id']."','".$attach_list."');\" style=\"cursor:pointer;\" alt=\"删\" /></span>
                      <span title=\"复制附件地址\" id=\"block_attach_".$attach_table."_".$file['id']."\"></span>
                    </td>
                    <td width=\"85%\" align=\"left\"><input type=\"text\" id=\"attach_".$attach_table."_".$file['id']."\" name=\"attach_".$attach_table."_".$file['id']."\" value=\"".$path.$file['filename']."\" onclick=\"this.select();\" class=\"input\" style=\"width:430px;\" /></td>
                  </tr>\n
                  <tr align=\"center\" bgcolor=\"#EEEEEE\">
                    <td width=\"15%\">".$file_icon."</td>
                    <td width=\"85%\" align=\"left\">".$file['brief']."</td>
                  </tr>\n";
                $str_js_han .= "$('#block_attach_".$attach_table."_".$file['id']."').bwcopyer({id: 'attach_".$attach_table."_".$file['id']."', get: 'attach_get', ret: 'attach_ret'});\n";
            }
            $result_str .= "</table>";
            $result_js = "\n$(document).ready(function() {\n".$str_js_han."});\n";
        }
        return array($result_str, $result_js);
    }
    //////

    // About friendlink
    // return_friendlink_info
    function return_friendlink_info($friendlink_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $sql = "SELECT friendlink.*,
                    detail.detail as detail, detail.page_type as detail_page_type, detail.page_word as detail_page_word,
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_FRIENDLINK." friendlink
                    LEFT JOIN ".DB_TABLE_FRIENDLINK_DETAIL." detail ON detail.friendlink_id=friendlink.id
                    LEFT JOIN ".DB_TABLE_FRIENDLINK_COVER." cover ON cover.friendlink_id=friendlink.id
                WHERE friendlink.id=".$friendlink_id."";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if (!$result->EOF) {
                $friendlink_info = array(
                        'id'                        => $result->fields['id'],
                        'phrase'                    => $result->fields['phrase'],
                        'phrase_len'                => mb_strwidth($result->fields['phrase'],"UTF-8"),
                        'subject'                   => $result->fields['subject'],
                        'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                        'prefix'                    => $result->fields['prefix'],
                        'color'                     => $result->fields['color'],
                        'fontstyle'                 => $result->fields['fontstyle'],
                        'decoration'                => $result->fields['decoration'],
                        'keyword'                   => $result->fields['keyword'],
                        'brief'                     => $result->fields['brief'],
                        'post_id'                   => $result->fields['post_id'],
                        'post_username'             => $result->fields['post_username'],
                        'post_ip'                   => $result->fields['post_ip'],
                        'post_time'                 => $result->fields['post_time'],
                        'amend_id'                  => $result->fields['amend_id'],
                        'amend_username'            => $result->fields['amend_username'],
                        'amend_ip'                  => $result->fields['amend_ip'],
                        'amend_time'                => $result->fields['amend_time'],
                        'click'                     => $result->fields['click'],
                        'if_enable'                 => $result->fields['if_enable'],
                        'if_enable_begin'           => $result->fields['if_enable_begin'],
                        'if_enable_end'             => $result->fields['if_enable_end'],
                        'if_top'                    => $result->fields['if_top'],
                        'if_top_begin'              => $result->fields['if_top_begin'],
                        'if_top_end'                => $result->fields['if_top_end'],
                        'if_turn'                   => $result->fields['if_turn'],
                        'if_turn_type'              => $result->fields['if_turn_type'],
                        'if_turn_url'               => $result->fields['if_turn_url'],
                        'rank'                      => $result->fields['rank'],

                        'detail'                    => $result->fields['detail'],
                        'page_type'                 => $result->fields['page_type'],
                        'page_word'                 => $result->fields['page_word'],

                        'cover_id'                  => $result->fields['cover_id'],
                        'cover_dir'                 => $result->fields['cover_dir'],
                        'cover_filename'            => $result->fields['cover_filename'],
                );

                $friendlink_info['detail_list']    = explode("[PAGESPLIT]", $friendlink_info['detail']);
                $friendlink_info['detail_num']     = count($friendlink_info['detail_list']);

                $friendlink_info['attach']         = func::db_select(DB_TABLE_FRIENDLINK_ATTACH, "*", "friendlink_id=".$result->fields['id'], "", "id");
            }
        }
        return $friendlink_info;
    }

    // return_friendlink_list
    function return_friendlink_list($limit="", $condition="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;

        $sql_where      = "";
        $sql_groupby    = "";
        $sql_orderby    = "";
        $sql_limit      = "";

        $friendlink_list   = array();

        if (!empty($condition['friendlink_id'])){
            if (!is_array($condition['friendlink_id'])) {
                $condition['friendlink_id'] = explode(",", $condition['friendlink_id']);
            }
            $condition['friendlink_id'] = func::array_clean($condition['friendlink_id']);
            if (is_array($condition['friendlink_id']) && count($condition['friendlink_id']) > 0){
                if (count($condition['friendlink_id']) > 1){
                    $sql_where .= " AND friendlink.id IN (".implode(",", $condition['friendlink_id']).")";
                } else {
                    $sql_where .= " AND friendlink.id=".implode(",", $condition['friendlink_id'])."";
                }
            }
        }

        if (!empty($condition['except_friendlink_id'])){
            if (!is_array($condition['except_friendlink_id'])) {
                $condition['except_friendlink_id'] = explode(",", $condition['except_friendlink_id']);
            }
            $condition['except_friendlink_id'] = func::array_clean($condition['except_friendlink_id']);
            if (is_array($condition['except_friendlink_id']) && count($condition['except_friendlink_id']) > 0){
                if (count($condition['except_friendlink_id']) > 1){
                    $sql_where .= " AND friendlink.id NOT IN (".implode(",", $condition['except_friendlink_id']).")";
                } else {
                    $sql_where .= " AND friendlink.id!=".implode(",", $condition['except_friendlink_id'])."";
                }
            }
        }

        if ($condition['if_top'] == "1") {
            $sql_where .= " AND ((friendlink.if_top=1) || (friendlink.if_top=3 AND friendlink.if_top_begin<".time()." AND friendlink.if_top_begin>".time()."))";
        }

        if ($condition['cover'] == "1") {
            $sql_where .= " AND cover.filename!=''";
        }

        $sql_where = substr(trim($sql_where), 0, 3) == "AND" ? substr(trim($sql_where), 3) : trim($sql_where);
        $sql_where = $sql_where != "" ? "WHERE ".$sql_where : "";

        if ($condition['click'] == "1"){
            $sql_orderby .= " click DESC,";
        }

        if ($condition['rand'] == "1"){
            $sql_orderby .= " rand(),";
        }

        if ((int)$limit > 0){
            $sql_limit .= " LIMIT ".(int)$limit;
        } else {
            $sql_limit .= " LIMIT ".$CONFIG['page_record_friendlink_list'];
        }

        $sql = "SELECT friendlink.*,
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_FRIENDLINK." friendlink
                    LEFT JOIN ".DB_TABLE_FRIENDLINK_COVER." cover ON cover.friendlink_id=friendlink.id
                ".$sql_where."
                ORDER BY ".$sql_orderby." friendlink.if_top, friendlink.rank, friendlink.post_time DESC ".$sql_limit;
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $friendlink_list[] = array(
                    'id'                        => $result->fields['id'],
                    'phrase'                    => $result->fields['phrase'],
                    'phrase_len'                => mb_strwidth($result->fields['phrase'],"UTF-8"),
                    'subject'                   => $result->fields['subject'],
                    'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                    'prefix'                    => $result->fields['prefix'],
                    'color'                     => $result->fields['color'],
                    'fontstyle'                 => $result->fields['fontstyle'],
                    'decoration'                => $result->fields['decoration'],
                    'keyword'                   => $result->fields['keyword'],
                    'brief'                     => $result->fields['brief'],
                    'post_id'                   => $result->fields['post_id'],
                    'post_username'             => $result->fields['post_username'],
                    'post_ip'                   => $result->fields['post_ip'],
                    'post_time'                 => $result->fields['post_time'],
                    'amend_id'                  => $result->fields['amend_id'],
                    'amend_username'            => $result->fields['amend_username'],
                    'amend_ip'                  => $result->fields['amend_ip'],
                    'amend_time'                => $result->fields['amend_time'],
                    'click'                     => $result->fields['click'],
                    'if_enable'                 => $result->fields['if_enable'],
                    'if_enable_begin'           => $result->fields['if_enable_begin'],
                    'if_enable_end'             => $result->fields['if_enable_end'],
                    'if_top'                    => $result->fields['if_top'],
                    'if_top_begin'              => $result->fields['if_top_begin'],
                    'if_top_end'                => $result->fields['if_top_end'],
                    'if_turn'                   => $result->fields['if_turn'],
                    'if_turn_type'              => $result->fields['if_turn_type'],
                    'if_turn_url'               => $result->fields['if_turn_url'],
                    'rank'                      => $result->fields['rank'],

                    'cover_id'                  => $result->fields['cover_id'],
                    'cover_dir'                 => $result->fields['cover_dir'],
                    'cover_filename'            => $result->fields['cover_filename'],
                );
                $result->MoveNext();
            }
        }
        return $friendlink_list;
    }

    // return_friendlink_attach_list
    function return_friendlink_attach_list($attach_list, $attach_type, $attach_where, $attach_table, $attach_dir)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $str_js_han     = "";
        $result_str     = "";
        $result_js      = "";
        $filelist       = func::db_select($attach_table, "*", $attach_where, "", "id DESC");
        if (is_array($filelist) && count($filelist) > 0){
            $result_str = "";
            $result_str .= "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\">";
            foreach($filelist as $file){
                $path   = $attach_dir."/".$file['dir'];
                if (file_exists($path.$file['filename'])) {
                    list($width, $height, $type, $attr) = @getimagesize($path.$file['filename']);
                }
                if (preg_match("/^(\.gif|\.jpg|\.png)$/i", substr($file['filename'], -4))){
                    $file_icon = "<img src=\"".CONFIG_ROOTDIR.$path.$file['filename']."\" width=\"45\" height=\"45\" border=\"0\" align=\"absmiddle\">";
                } else {
                    $file_icon = "&nbsp;";
                }
                if ($attach_type == "add") { $file['friendlink_id'] = $MEMBER['id']; }
                $result_str .= "
                  <tr align=\"center\" bgcolor=\"#DEDEDE\">
                    <td width=\"15%\">
                      <span title=\"删除附件\"><img src=\"".$SETTING['dir_images_admin']."/icon_delete.gif\" onclick=\"xajax_attach_del('".$attach_type."','".$file['friendlink_id']."','".$file['id']."','".$attach_list."');\" style=\"cursor:pointer;\" alt=\"删\" /></span>
                      <span title=\"复制附件地址\" id=\"block_attach_".$attach_table."_".$file['id']."\"></span>
                    </td>
                    <td width=\"85%\" align=\"left\"><input type=\"text\" id=\"attach_".$attach_table."_".$file['id']."\" name=\"attach_".$attach_table."_".$file['id']."\" value=\"".$path.$file['filename']."\" onclick=\"this.select();\" class=\"input\" style=\"width:430px;\" /></td>
                  </tr>\n
                  <tr align=\"center\" bgcolor=\"#EEEEEE\">
                    <td width=\"15%\">".$file_icon."</td>
                    <td width=\"85%\" align=\"left\">".$file['brief']."</td>
                  </tr>\n";
                $str_js_han .= "$('#block_attach_".$attach_table."_".$file['id']."').bwcopyer({id: 'attach_".$attach_table."_".$file['id']."', get: 'attach_get', ret: 'attach_ret'});\n";
            }
            $result_str .= "</table>";
            $result_js = "\n$(document).ready(function() {\n".$str_js_han."});\n";
        }
        return array($result_str, $result_js);
    }
    //////

    // About topic
    // return_topic_info
    function return_topic_info($topic_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $sql_select     = "";
        $sql_leftjoin   = "";
        if ($ADMIN['iflogin'] == "1") {
            $sql_select     .= "channel_manage.admin_id as channel_manage_admin_id,";
            $sql_leftjoin   .= "LEFT JOIN ".DB_TABLE_CHANNEL_MANAGE." channel_manage ON channel_manage.channel_id=channel.id AND channel_manage.admin_id=".$ADMIN['mid'];
        }
        $sql = "SELECT topic.*,
                    ".$sql_select."
                    channel.id as channel_id, channel.name as channel_name
                FROM ".DB_TABLE_TOPIC." topic
                    LEFT JOIN ".DB_TABLE_CHANNEL." channel ON channel.id=topic.channel_id
                    ".$sql_leftjoin."
                WHERE topic.id=".$topic_id."";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if (!$result->EOF) {
                $topic_info = array(
                    'id'                        => $result->fields['id'],
                    'channel_id'                => $result->fields['channel_id'],
                    'icon'                      => $result->fields['icon'],
                    'subject'                   => $result->fields['subject'],
                    'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                    'post_time'                 => $result->fields['post_time'],
                    'post_mid'                  => $result->fields['post_mid'],
                    'post_musername'            => $result->fields['post_musername'],
                    'post_ip'                   => $result->fields['post_ip'],
                    'post_pid'                  => $result->fields['post_pid'],
                    'number_reply'              => $result->fields['number_reply'],
                    'number_click'              => $result->fields['number_click'],
                    'last_time'                 => $result->fields['last_time'],
                    'last_mid'                  => $result->fields['last_mid'],
                    'last_musername'            => $result->fields['last_musername'],
                    'last_pid'                  => $result->fields['last_pid'],
                    'style_color'               => $result->fields['style_color'],
                    'style_fontstyle'           => $result->fields['style_fontstyle'],
                    'style_decoration'          => $result->fields['style_decoration'],
                    'style_begin'               => $result->fields['style_begin'],
                    'style_end'                 => $result->fields['style_end'],
                    'if_elite'                  => $result->fields['if_elite'],
                    'if_elite_begin'            => $result->fields['if_elite_begin'],
                    'if_elite_end'              => $result->fields['if_elite_end'],
                    'if_top'                    => $result->fields['if_top'],
                    'if_top_begin'              => $result->fields['if_top_begin'],
                    'if_top_end'                => $result->fields['if_top_end'],
                    'if_close'                  => $result->fields['if_close'],
                    'if_close_begin'            => $result->fields['if_close_begin'],
                    'if_close_end'              => $result->fields['if_close_end'],
                    'if_move'                   => $result->fields['if_move'],
                    'if_move_id'                => $result->fields['if_move_id'],
                    'if_del'                    => $result->fields['if_del'],
                    'rank'                      => $result->fields['rank'],

                    'channel_id'                => $result->fields['channel_id'],
                    'channel_name'              => $result->fields['channel_name'],
                    'channel_name_len'          => mb_strwidth($result->fields['channel_name'],"UTF-8"),

                    'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                );
            }
        }
        return $topic_info;
    }

    // return_topic_list
    function return_topic_list($channel_id="", $limit="", $condition="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;

        $sql_select     = "";
        $sql_leftjoin   = "";
        $sql_where      = "";
        $sql_groupby    = "";
        $sql_orderby    = "";
        $sql_limit      = "";

        $topic_list     = array();

        if (!empty($channel_id)){
            if (!is_array($channel_id)) {
                $channel_id = explode(",", $channel_id);
            }
            $channel_id = func::array_clean($channel_id);
            if (is_array($channel_id) && count($channel_id) > 0){
                if (count($channel_id) > 1){
                    $sql_where .= " AND topic.channel_id IN (".implode(",", $channel_id).")";
                } else {
                    $sql_where .= " AND topic.channel_id=".implode(",", $channel_id)."";
                }
            }
        } elseif (!empty($condition['channel_id'])){
            if (!is_array($condition['channel_id'])) {
                $condition['channel_id'] = explode(",", $condition['channel_id']);
            }
            $condition['channel_id'] = func::array_clean($condition['channel_id']);
            if (is_array($condition['channel_id']) && count($condition['channel_id']) > 0){
                if (count($condition['channel_id']) > 1){
                    $sql_where .= " AND topic.channel_id IN (".implode(",", $condition['channel_id']).")";
                } else {
                    $sql_where .= " AND topic.channel_id=".implode(",", $condition['channel_id'])."";
                }
            }
        }

        if (!empty($condition['except_channel_id'])){
            if (!is_array($condition['except_channel_id'])) {
                $condition['except_channel_id'] = explode(",", $condition['except_channel_id']);
            }
            $condition['except_channel_id'] = func::array_clean($condition['except_channel_id']);
            if (is_array($condition['except_channel_id']) && count($condition['except_channel_id']) > 0){
                if (count($condition['except_channel_id']) > 1){
                    $sql_where .= " AND topic.channel_id NOT IN (".implode(",", $condition['except_channel_id']).")";
                } else {
                    $sql_where .= " AND topic.channel_id!=".implode(",", $condition['except_channel_id'])."";
                }
            }
        }

        if (!empty($condition['topic_id'])){
            if (!is_array($condition['topic_id'])) {
                $condition['topic_id'] = explode(",", $condition['topic_id']);
            }
            $condition['topic_id'] = func::array_clean($condition['topic_id']);
            if (is_array($condition['topic_id']) && count($condition['topic_id']) > 0){
                if (count($condition['topic_id']) > 1){
                    $sql_where .= " AND topic.id IN (".implode(",", $condition['topic_id']).")";
                } else {
                    $sql_where .= " AND topic.id=".implode(",", $condition['topic_id'])."";
                }
            }
        }

        if (!empty($condition['except_topic_id'])){
            if (!is_array($condition['except_topic_id'])) {
                $condition['except_topic_id'] = explode(",", $condition['except_topic_id']);
            }
            $condition['except_topic_id'] = func::array_clean($condition['except_topic_id']);
            if (is_array($condition['except_topic_id']) && count($condition['except_topic_id']) > 0){
                if (count($condition['except_topic_id']) > 1){
                    $sql_where .= " AND topic.id NOT IN (".implode(",", $condition['except_topic_id']).")";
                } else {
                    $sql_where .= " AND topic.id!=".implode(",", $condition['except_topic_id'])."";
                }
            }
        }

        if ($condition['if_elite'] == "1") {
            $sql_where .= " AND ((topic.if_elite=1) || (topic.if_elite=3 AND topic.if_elite_begin<".time()." AND topic.if_elite_end>".time()."))";
        }

        if ($condition['if_top'] == "1") {
            $sql_where .= " AND ((topic.if_top=1) || (topic.if_top=3 AND topic.if_top_begin<".time()." AND topic.if_top_end>".time()."))";
        }

        if ($condition['if_close'] == "1") {
            $sql_where .= " AND ((topic.if_close=1) || (topic.if_close=3 AND topic.if_close_begin<".time()." AND topic.if_close_end>".time()."))";
        }

        $sql_where .= " AND (topic.if_del!=1)";

        $sql_where = substr(trim($sql_where), 0, 3) == "AND" ? substr(trim($sql_where), 3) : trim($sql_where);
        $sql_where = $sql_where != "" ? "WHERE ".$sql_where : "";

        if ($condition['click'] == "1"){
            $sql_orderby .= " click DESC,";
        }

        if ($condition['rand'] == "1"){
            $sql_orderby .= " rand(),";
        }

        if ((int)$limit > 0){
            $sql_limit = " LIMIT ".(int)$limit;
        } else {
            $sql_limit = " LIMIT ".$CONFIG['page_record_topic_list'];
        }

        if ($ADMIN['iflogin'] == "1") {
            $sql_select     .= "channel_manage.admin_id as channel_manage_admin_id,";
            $sql_leftjoin   .= "LEFT JOIN ".DB_TABLE_CHANNEL_MANAGE." channel_manage ON channel_manage.channel_id=channel.id AND channel_manage.admin_id=".$ADMIN['mid'];
        }
        $sql = "SELECT topic.*,
                    ".$sql_select."
                    channel.id as channel_id, channel.name as channel_name
                FROM ".DB_TABLE_TOPIC." topic
                    LEFT JOIN ".DB_TABLE_CHANNEL." channel ON channel.id=topic.channel_id
                    ".$sql_leftjoin."
                ".$sql_where."
                ORDER BY ".$sql_orderby." topic.if_top DESC, topic.rank DESC, topic.id DESC ".$sql_limit;
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $topic_list[] = array(
                    'id'                        => $result->fields['id'],
                    'channel_id'                => $result->fields['channel_id'],
                    'icon'                      => $result->fields['icon'],
                    'subject'                   => $result->fields['subject'],
                    'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                    'post_mid'                  => $result->fields['post_mid'],
                    'post_musername'            => $result->fields['post_musername'],
                    'post_ip'                   => $result->fields['post_ip'],
                    'post_time'                 => $result->fields['post_time'],
                    'post_pid'                  => $result->fields['post_pid'],
                    'number_reply'              => $result->fields['number_reply'],
                    'number_click'              => $result->fields['number_click'],
                    'last_mid'                  => $result->fields['last_mid'],
                    'last_musername'            => $result->fields['last_musername'],
                    'last_time'                 => $result->fields['last_time'],
                    'last_pid'                  => $result->fields['last_pid'],
                    'style_color'               => $result->fields['style_color'],
                    'style_fontstyle'           => $result->fields['style_fontstyle'],
                    'style_decoration'          => $result->fields['style_decoration'],
                    'style_begin'               => $result->fields['style_begin'],
                    'style_end'                 => $result->fields['style_end'],
                    'if_elite'                  => $result->fields['if_elite'],
                    'if_elite_begin'            => $result->fields['if_elite_begin'],
                    'if_elite_end'              => $result->fields['if_elite_end'],
                    'if_top'                    => $result->fields['if_top'],
                    'if_top_begin'              => $result->fields['if_top_begin'],
                    'if_top_end'                => $result->fields['if_top_end'],
                    'if_close'                  => $result->fields['if_close'],
                    'if_close_begin'            => $result->fields['if_close_begin'],
                    'if_close_end'              => $result->fields['if_close_end'],
                    'if_move'                   => $result->fields['if_move'],
                    'if_move_id'                => $result->fields['if_move_id'],
                    'if_del'                    => $result->fields['if_del'],
                    'rank'                      => $result->fields['rank'],

                    'channel_id'                => $result->fields['channel_id'],
                    'channel_name'              => $result->fields['channel_name'],
                    'channel_name_len'          => mb_strwidth($result->fields['channel_name'],"UTF-8"),

                    'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                );
                $result->MoveNext();
            }
        }
        return $topic_list;
    }

    // return_channel_info
    function return_channel_info($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $sql_select     = "";
        $sql_leftjoin   = "";
        if ($ADMIN['iflogin'] == "1") {
            $sql_select     .= "channel_manage.admin_id as channel_manage_admin_id,";
            $sql_leftjoin   .= "LEFT JOIN ".DB_TABLE_CHANNEL_MANAGE." channel_manage ON channel_manage.channel_id=channel.id AND channel_manage.admin_id=".$ADMIN['mid'];
        }
        $sql = "SELECT channel.*,
                    ".$sql_select."
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_CHANNEL." channel
                    LEFT JOIN ".DB_TABLE_CHANNEL_COVER." cover ON cover.channel_id=channel.id
                    ".$sql_leftjoin."
                WHERE channel.id=".$channel_id."";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if (!$result->EOF) {
                $channel_info = array(
                    'id'                        => $result->fields['id'],
                    'type'                      => $result->fields['type'],
                    'upper_id'                  => $result->fields['upper_id'],
                    'path'                      => $result->fields['path'],
                    'name'                      => $result->fields['name'],
                    'password'                  => $result->fields['password'],
                    'brief'                     => $result->fields['brief'],
                    'notice'                    => $result->fields['notice'],
                    'keyword'                   => $result->fields['keyword'],
                    'template_channel'          => $result->fields['template_channel'],
                    'template_topic'            => $result->fields['template_topic'],
                    'page_record'               => $result->fields['page_record'],
                    'number_total_topic'        => $result->fields['number_total_topic'],
                    'number_total_post'         => $result->fields['number_total_post'],
                    'number_today_topic'        => $result->fields['number_today_topic'],
                    'number_today_post'         => $result->fields['number_today_post'],
                    'last_topic_id'             => $result->fields['last_topic_id'],
                    'last_topic_subject'        => $result->fields['last_topic_subject'],
                    'last_topic_mid'            => $result->fields['last_topic_mid'],
                    'last_topic_musername'      => $result->fields['last_topic_musername'],
                    'last_topic_time'           => $result->fields['last_topic_time'],
                    'last_post_id'              => $result->fields['last_post_id'],
                    'last_post_topic_id'        => $result->fields['last_post_topic_id'],
                    'last_post_mid'             => $result->fields['last_post_mid'],
                    'last_post_musername'       => $result->fields['last_post_musername'],
                    'last_post_time'            => $result->fields['last_post_time'],
                    'if_enable'                 => $result->fields['if_enable'],
                    'if_lock'                   => $result->fields['if_lock'],
                    'if_show'                   => $result->fields['if_show'],
                    'if_recommend'              => $result->fields['if_recommend'],
                    'if_add'                    => $result->fields['if_add'],
                    'if_guest'                  => $result->fields['if_guest'],
                    'if_anonymous'              => $result->fields['if_anonymous'],
                    'if_splitword'              => $result->fields['if_splitword'],
                    'if_face'                   => $result->fields['if_face'],
                    'if_bbcode'                 => $result->fields['if_bbcode'],
                    'if_url'                    => $result->fields['if_url'],
                    'if_html'                   => $result->fields['if_html'],
                    'if_turn'                   => $result->fields['if_turn'],
                    'if_turn_type'              => $result->fields['if_turn_type'],
                    'if_turn_url'               => $result->fields['if_turn_url'],
                    'rank'                      => $result->fields['rank'],

                    'cover_id'                  => $result->fields['cover_id'],
                    'cover_dir'                 => $result->fields['cover_dir'],
                    'cover_filename'            => $result->fields['cover_filename'],

                    'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                );
            }
        }
        return $channel_info;
    }

    // return_channel_manage
    function return_channel_manage($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $sql = "SELECT channel_manage.*,
                    member.username as username,
                    s.value as color_value,s.name as color_name
                FROM ".DB_TABLE_CHANNEL_MANAGE." channel_manage
                    LEFT JOIN ".DB_TABLE_MEMBER." member ON member.id=channel_manage.admin_id
                    LEFT JOIN ".DB_TABLE_SELECT." s ON s.id=channel_manage.color
                WHERE channel_id=".$channel_id."
                ORDER BY channel_manage.rank, channel_manage.admin_id";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }else {
            while(!$result->EOF){
                $channel_manage_list[] = array(
                    'channel_id'    => $result->fields['channel_id'],
                    'mid'           => $result->fields['mid'],
                    'color'         => $result->fields['color'],
                    'bold'          => $result->fields['bold'],
                    'rank'          => $result->fields['rank'],
                    'time_created'  => $result->fields['time_created'],

                    'username'      => $result->fields['username'],

                    'color_value'   => $result->fields['color_value'],
                    'color_name'    => $result->fields['color_name'],
                );
                $result->MoveNext();
            }
        }
        return $channel_manage_list;
    }

    // return_channel_all
    function return_channel_all()
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $this->channel_all      = array();
        $this->channel_upper    = array();
        $sql_select     = "";
        $sql_leftjoin   = "";
        if ($ADMIN['iflogin'] == "1") {
            $sql_select     .= "channel_manage.admin_id as channel_manage_admin_id,";
            $sql_leftjoin   .= "LEFT JOIN ".DB_TABLE_CHANNEL_MANAGE." channel_manage ON channel_manage.channel_id=channel.id AND channel_manage.admin_id=".$ADMIN['mid'];
        }
        $sql = "SELECT channel.*,
                    ".$sql_select."
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_CHANNEL." channel
                    LEFT JOIN ".DB_TABLE_CHANNEL_COVER." cover ON cover.channel_id=channel.id
                    ".$sql_leftjoin."
                WHERE channel.if_enable=1
                ORDER BY channel.rank, channel.id";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $this->channel_all[$result->fields['id']] = array(
                    'id'                        => $result->fields['id'],
                    'type'                      => $result->fields['type'],
                    'upper_id'                  => $result->fields['upper_id'],
                    'path'                      => $result->fields['path'],
                    'name'                      => $result->fields['name'],
                    'password'                  => $result->fields['password'],
                    'brief'                     => $result->fields['brief'],
                    'notice'                    => $result->fields['notice'],
                    'keyword'                   => $result->fields['keyword'],
                    'template_channel'          => $result->fields['template_channel'],
                    'template_topic'            => $result->fields['template_topic'],
                    'page_record'               => $result->fields['page_record'],
                    'number_total_topic'        => $result->fields['number_total_topic'],
                    'number_total_post'         => $result->fields['number_total_post'],
                    'number_today_topic'        => $result->fields['number_today_topic'],
                    'number_today_post'         => $result->fields['number_today_post'],
                    'last_topic_id'             => $result->fields['last_topic_id'],
                    'last_topic_subject'        => $result->fields['last_topic_subject'],
                    'last_topic_mid'            => $result->fields['last_topic_mid'],
                    'last_topic_musername'      => $result->fields['last_topic_musername'],
                    'last_topic_time'           => $result->fields['last_topic_time'],
                    'last_post_id'              => $result->fields['last_post_id'],
                    'last_post_topic_id'        => $result->fields['last_post_topic_id'],
                    'last_post_mid'             => $result->fields['last_post_mid'],
                    'last_post_musername'       => $result->fields['last_post_musername'],
                    'last_post_time'            => $result->fields['last_post_time'],
                    'if_enable'                 => $result->fields['if_enable'],
                    'if_lock'                   => $result->fields['if_lock'],
                    'if_show'                   => $result->fields['if_show'],
                    'if_recommend'              => $result->fields['if_recommend'],
                    'if_add'                    => $result->fields['if_add'],
                    'if_guest'                  => $result->fields['if_guest'],
                    'if_anonymous'              => $result->fields['if_anonymous'],
                    'if_splitword'              => $result->fields['if_splitword'],
                    'if_face'                   => $result->fields['if_face'],
                    'if_bbcode'                 => $result->fields['if_bbcode'],
                    'if_url'                    => $result->fields['if_url'],
                    'if_html'                   => $result->fields['if_html'],
                    'if_turn'                   => $result->fields['if_turn'],
                    'if_turn_type'              => $result->fields['if_turn_type'],
                    'if_turn_url'               => $result->fields['if_turn_url'],
                    'rank'                      => $result->fields['rank'],

                    'cover_id'                  => $result->fields['cover_id'],
                    'cover_dir'                 => $result->fields['cover_dir'],
                    'cover_filename'            => $result->fields['cover_filename'],

                    'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                );
                $this->channel_upper[$result->fields['upper_id']][] = array(
                    'id'                        => $result->fields['id'],
                    'type'                      => $result->fields['type'],
                    'upper_id'                  => $result->fields['upper_id'],
                    'path'                      => $result->fields['path'],
                    'name'                      => $result->fields['name'],
                    'password'                  => $result->fields['password'],
                    'brief'                     => $result->fields['brief'],
                    'notice'                    => $result->fields['notice'],
                    'keyword'                   => $result->fields['keyword'],
                    'template_channel'          => $result->fields['template_channel'],
                    'template_topic'            => $result->fields['template_topic'],
                    'page_record'               => $result->fields['page_record'],
                    'number_total_topic'        => $result->fields['number_total_topic'],
                    'number_total_post'         => $result->fields['number_total_post'],
                    'number_today_topic'        => $result->fields['number_today_topic'],
                    'number_today_post'         => $result->fields['number_today_post'],
                    'last_topic_id'             => $result->fields['last_topic_id'],
                    'last_topic_subject'        => $result->fields['last_topic_subject'],
                    'last_topic_mid'            => $result->fields['last_topic_mid'],
                    'last_topic_musername'      => $result->fields['last_topic_musername'],
                    'last_topic_time'           => $result->fields['last_topic_time'],
                    'last_post_id'              => $result->fields['last_post_id'],
                    'last_post_topic_id'        => $result->fields['last_post_topic_id'],
                    'last_post_mid'             => $result->fields['last_post_mid'],
                    'last_post_musername'       => $result->fields['last_post_musername'],
                    'last_post_time'            => $result->fields['last_post_time'],
                    'if_enable'                 => $result->fields['if_enable'],
                    'if_lock'                   => $result->fields['if_lock'],
                    'if_show'                   => $result->fields['if_show'],
                    'if_recommend'              => $result->fields['if_recommend'],
                    'if_add'                    => $result->fields['if_add'],
                    'if_guest'                  => $result->fields['if_guest'],
                    'if_anonymous'              => $result->fields['if_anonymous'],
                    'if_splitword'              => $result->fields['if_splitword'],
                    'if_face'                   => $result->fields['if_face'],
                    'if_bbcode'                 => $result->fields['if_bbcode'],
                    'if_url'                    => $result->fields['if_url'],
                    'if_html'                   => $result->fields['if_html'],
                    'if_turn'                   => $result->fields['if_turn'],
                    'if_turn_type'              => $result->fields['if_turn_type'],
                    'if_turn_url'               => $result->fields['if_turn_url'],
                    'rank'                      => $result->fields['rank'],

                    'cover_id'                  => $result->fields['cover_id'],
                    'cover_dir'                 => $result->fields['cover_dir'],
                    'cover_filename'            => $result->fields['cover_filename'],

                    'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                );
                $result->MoveNext();
            }
        }
        return true;
    }

    // return_channel_current
    function return_channel_current($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $this->channel_current  = array();

        $channel_id             = (int)$channel_id;

        if ($channel_id != "0") {
            $sql = "SELECT path FROM ".DB_TABLE_CHANNEL." WHERE id=".$channel_id."";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
            } else {
                if (!$result->EOF) {
                    $path = $result->fields['path'];
                }
            }
            $array_path = explode(",", $path);
            $sql = "SELECT id, type, upper_id, name, keyword, if_recommend
                    FROM ".DB_TABLE_CHANNEL."
                    WHERE id IN (".$path.")";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
            } else {
                while (!$result->EOF) {
                    $current[$result->fields['id']] = array(
                            'id'            => $result->fields['id'],
                            'type'          => $result->fields['type'],
                            'upper_id'      => $result->fields['upper_id'],
                            'name'          => $result->fields['name'],
                            'keyword'       => $result->fields['keyword'],
                            'if_recommend'  => $result->fields['if_recommend'],
                        );
                    $result->MoveNext();
                }
            }
            foreach ($array_path as $v) {
                $this->channel_current[] = $current[$v];
            }
        }
        return $this->channel_current;
    }

    // return_channel_sub
    function return_channel_sub($channel_id, $limit="")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $this->channel_sub  = array();

        $channel_id         = (int)$channel_id;
        $limit              = (int)$limit;

        $sql_select     = "";
        $sql_leftjoin   = "";
        if ($ADMIN['iflogin'] == "1") {
            $sql_select     .= "channel_manage.admin_id as channel_manage_admin_id,";
            $sql_leftjoin   .= "LEFT JOIN ".DB_TABLE_CHANNEL_MANAGE." channel_manage ON channel_manage.channel_id=channel.id AND channel_manage.admin_id=".$ADMIN['mid'];
        }

        if( (int)$limit > 0 ){
            $sql_limit = " LIMIT ".(int)$limit;
        }
        $sql = "SELECT channel.*,
                    ".$sql_select."
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_CHANNEL." channel
                    LEFT JOIN ".DB_TABLE_CHANNEL_COVER." cover ON cover.channel_id=channel.id
                    ".$sql_leftjoin."
                WHERE channel.upper_id=".$channel_id." ORDER BY channel.rank, channel.id ".$sql_limit;
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $this->channel_sub[$result->fields['id']] = array(
                    'id'                        => $result->fields['id'],
                    'type'                      => $result->fields['type'],
                    'upper_id'                  => $result->fields['upper_id'],
                    'path'                      => $result->fields['path'],
                    'name'                      => $result->fields['name'],
                    'password'                  => $result->fields['password'],
                    'notice'                    => $result->fields['notice'],
                    'keyword'                   => $result->fields['keyword'],
                    'template_channel'          => $result->fields['template_channel'],
                    'template_topic'            => $result->fields['template_topic'],
                    'page_record'               => $result->fields['page_record'],
                    'number_total_topic'        => $result->fields['number_total_topic'],
                    'number_total_post'         => $result->fields['number_total_post'],
                    'number_today_topic'        => $result->fields['number_today_topic'],
                    'number_today_post'         => $result->fields['number_today_post'],
                    'last_topic_id'             => $result->fields['last_topic_id'],
                    'last_topic_subject'        => $result->fields['last_topic_subject'],
                    'last_topic_mid'            => $result->fields['last_topic_mid'],
                    'last_topic_musername'      => $result->fields['last_topic_musername'],
                    'last_topic_time'           => $result->fields['last_topic_time'],
                    'last_post_id'              => $result->fields['last_post_id'],
                    'last_post_topic_id'        => $result->fields['last_post_topic_id'],
                    'last_post_mid'             => $result->fields['last_post_mid'],
                    'last_post_musername'       => $result->fields['last_post_musername'],
                    'last_post_time'            => $result->fields['last_post_time'],
                    'if_enable'                 => $result->fields['if_enable'],
                    'if_lock'                   => $result->fields['if_lock'],
                    'if_show'                   => $result->fields['if_show'],
                    'if_recommend'              => $result->fields['if_recommend'],
                    'if_add'                    => $result->fields['if_add'],
                    'if_guest'                  => $result->fields['if_guest'],
                    'if_anonymous'              => $result->fields['if_anonymous'],
                    'if_splitword'              => $result->fields['if_splitword'],
                    'if_face'                   => $result->fields['if_face'],
                    'if_bbcode'                 => $result->fields['if_bbcode'],
                    'if_url'                    => $result->fields['if_url'],
                    'if_html'                   => $result->fields['if_html'],
                    'if_turn'                   => $result->fields['if_turn'],
                    'if_turn_type'              => $result->fields['if_turn_type'],
                    'if_turn_url'               => $result->fields['if_turn_url'],
                    'rank'                      => $result->fields['rank'],

                    'cover_id'                  => $result->fields['cover_id'],
                    'cover_dir'                 => $result->fields['cover_dir'],
                    'cover_filename'            => $result->fields['cover_filename'],

                    'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                );
                $sub_record_sql     = "SELECT count(*) as sub_record FROM ".DB_TABLE_CHANNEL." WHERE upper_id=".$result->fields['id']."";
                $sub_record_result  = &$db->Execute($sub_record_sql);
                if (!$sub_record_result) {
                    echo $db->ErrorMsg();
                    $sub_record = 0;
                } else {
                    $sub_record = $sub_record_result->fields['sub_record'];
                }
                $this->channel_sub[$result->fields['id']]['sub_record'] = (int)$sub_record;
                $result->MoveNext();
            }
        }
        return $this->channel_sub;
    }

    // return_channel_allsub
    function return_channel_allsub($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $this->channel_allsub = array();

        $channel_id         = (int)$channel_id;

        $sql_select     = "";
        $sql_leftjoin   = "";
        if ($ADMIN['iflogin'] == "1") {
            $sql_select     .= "channel_manage.admin_id as channel_manage_admin_id,";
            $sql_leftjoin   .= "LEFT JOIN ".DB_TABLE_CHANNEL_MANAGE." channel_manage ON channel_manage.channel_id=channel.id AND channel_manage.admin_id=".$ADMIN['mid'];
        }

        if ($channel_id != "0") {
            //$sql = "SELECT id FROM ".DB_TABLE_CHANNEL." WHERE if_enable=1 AND path LIKE '%,".$channel_id.",%' ORDER BY rank,id";
            $sql = "SELECT channel.*,
                        ".$sql_select."
                        cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                    FROM ".DB_TABLE_CHANNEL." channel
                        LEFT JOIN ".DB_TABLE_CHANNEL_COVER." cover ON cover.channel_id=channel.id
                        ".$sql_leftjoin."
                    WHERE FIND_IN_SET(".$channel_id.", channel.path) > 0 ORDER BY channel.rank, channel.id";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
            } else {
                while (!$result->EOF) {
                    $this->channel_allsub[$result->fields['id']] = array(
                        'id'                        => $result->fields['id'],
                        'type'                      => $result->fields['type'],
                        'upper_id'                  => $result->fields['upper_id'],
                        'path'                      => $result->fields['path'],
                        'name'                      => $result->fields['name'],
                        'password'                  => $result->fields['password'],
                        'notice'                    => $result->fields['notice'],
                        'keyword'                   => $result->fields['keyword'],
                        'template_channel'          => $result->fields['template_channel'],
                        'template_topic'            => $result->fields['template_topic'],
                        'page_record'               => $result->fields['page_record'],
                        'number_total_topic'        => $result->fields['number_total_topic'],
                        'number_total_post'         => $result->fields['number_total_post'],
                        'number_today_topic'        => $result->fields['number_today_topic'],
                        'number_today_post'         => $result->fields['number_today_post'],
                        'last_topic_id'             => $result->fields['last_topic_id'],
                        'last_topic_subject'        => $result->fields['last_topic_subject'],
                        'last_topic_mid'            => $result->fields['last_topic_mid'],
                        'last_topic_musername'      => $result->fields['last_topic_musername'],
                        'last_topic_time'           => $result->fields['last_topic_time'],
                        'last_post_id'              => $result->fields['last_post_id'],
                        'last_post_topic_id'        => $result->fields['last_post_topic_id'],
                        'last_post_mid'             => $result->fields['last_post_mid'],
                        'last_post_musername'       => $result->fields['last_post_musername'],
                        'last_post_time'            => $result->fields['last_post_time'],
                        'if_enable'                 => $result->fields['if_enable'],
                        'if_lock'                   => $result->fields['if_lock'],
                        'if_show'                   => $result->fields['if_show'],
                        'if_recommend'              => $result->fields['if_recommend'],
                        'if_add'                    => $result->fields['if_add'],
                        'if_guest'                  => $result->fields['if_guest'],
                        'if_anonymous'              => $result->fields['if_anonymous'],
                        'if_splitword'              => $result->fields['if_splitword'],
                        'if_face'                   => $result->fields['if_face'],
                        'if_bbcode'                 => $result->fields['if_bbcode'],
                        'if_url'                    => $result->fields['if_url'],
                        'if_html'                   => $result->fields['if_html'],
                        'if_turn'                   => $result->fields['if_turn'],
                        'if_turn_type'              => $result->fields['if_turn_type'],
                        'if_turn_url'               => $result->fields['if_turn_url'],
                        'rank'                      => $result->fields['rank'],

                        'cover_id'                  => $result->fields['cover_id'],
                        'cover_dir'                 => $result->fields['cover_dir'],
                        'cover_filename'            => $result->fields['cover_filename'],

                        'channel_manage_admin_id'        => $result->fields['channel_manage_admin_id'],
                    );
                    $result->MoveNext();
                }
            }
        }
        return $this->channel_allsub;
    }

    // return_channel_id_allsub
    function return_channel_id_allsub($channel_id, $cache)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $channel_id_allsub = array();

        $channel_id         = (int)$channel_id;

        if ($channel_id != "0") {
            $sql = "SELECT id
                    FROM ".DB_TABLE_CHANNEL."
                    WHERE FIND_IN_SET(".$channel_id.", path) > 0 ORDER BY rank, id";
            if ( $cache > 0 ) {
                $db->cacheSecs = $cache;
                $result = &$db->CacheExecute($sql);
            } else {
                $result = &$db->Execute($sql);
            }
            if (!$result) {
                echo $db->ErrorMsg();
            } else {
                while (!$result->EOF) {
                    $channel_id_allsub[$result->fields['id']] = $result->fields['id'];
                    $result->MoveNext();
                }
            }
        }
        return $channel_id_allsub;
    }

    // return_channel_tree
    function return_channel_tree($default = "", $upper_id = 0, $depth = 0)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        if ( empty($this->channel_all) ) { $this->return_channel_all(); }
        if ( is_array($this->channel_all) ) {
            foreach ($this->channel_all as $key => $list) {
                if ($list['upper_id'] == $upper_id) {
                    if ($default != "" && $default == $list['id']) {
                        $list['depth']       = $depth;
                        $list['selected']    = "1";
                        $this->channel_tree[$list['id']] = $list;
                        $depth++;
                        $this->return_channel_tree($default, $list['id'], $depth);
                        $depth--;
                    }else{
                        $list['depth']       = $depth;
                        $this->channel_tree[$list['id']] = $list;
                        $depth++;
                        $this->return_channel_tree($default, $list['id'], $depth);
                        $depth--;
                    }
                }
            }
        }
        return $this->channel_tree;
    }

    // update_channel_path
    function update_channel_path($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $this->channel_tree = array();

        $channel_id = (int)$channel_id;

        $this->return_channel_tree("", $channel_id);
        $allsub = array_merge((array)$channel_id, array_keys($this->channel_tree));

        foreach ($allsub as $v) {
            $upper_id = $v;
            $path     = array();
            $if_end   = false;
            while($if_end === false) {
                $sql = "SELECT id,upper_id FROM ".DB_TABLE_CHANNEL." WHERE id=".$upper_id."";
                $result = &$db->Execute($sql);
                if (!$result) {
                    echo $db->ErrorMsg();
                    $if_end = true;
                } else {
                    if (!$result->EOF) {
                        $path[]   = $result->fields['id'];
                        $upper_id = $result->fields['upper_id'];
                    }else{
                        $if_end = true;
                    }
                }
            }
            krsort($path);
            $update_sql = "UPDATE ".DB_TABLE_CHANNEL." SET
                               path = '".addslashes(implode(",", $path))."'
                           WHERE id=".$v."";
            $update_result = $db->Execute($update_sql);
            if (!$update_result) {
                echo $db->ErrorMsg();
            }
        }
        return $allsub;
    }

    // update_channel_last
    function update_channel_last($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;

        $last_topic = func::db_select(DB_TABLE_TOPIC, "id,subject,post_mid,post_musername,post_time", "channel_id=".(int)$channel_id."", "1", "rank DESC,id DESC");
        if (!empty($last_topic)) {
            $sql_data['last_topic_id']          = "'".(int)$last_topic[0]['id']."'";
            $sql_data['last_topic_subject']     = "'".$last_topic[0]['subject']."'";
            $sql_data['last_topic_mid']         = "'".(int)$last_topic[0]['post_mid']."'";
            $sql_data['last_topic_musername']   = "'".$last_topic[0]['post_musername']."'";
            $sql_data['last_topic_time']        = "'".(int)$last_topic[0]['post_time']."'";

            $last_reply = func::db_select(DB_TABLE_POST, "id,topic_id,post_mid,post_musername,post_time", "topic_id=".(int)$last_topic[0]['id']."", "1", "rank DESC,id DESC");
            if (!empty($last_reply)) {
                $sql_data['last_post_id']           = "'".(int)$last_reply[0]['id']."'";
                $sql_data['last_post_topic_id']     = "'".$last_reply[0]['topic_id']."'";
                $sql_data['last_post_mid']          = "'".(int)$last_reply[0]['post_mid']."'";
                $sql_data['last_post_musername']    = "'".$last_reply[0]['post_musername']."'";
                $sql_data['last_post_time']         = "'".(int)$last_reply[0]['post_time']."'";
            } else {
                $sql_data['last_post_id']           = "NULL";
                $sql_data['last_post_topic_id']     = "NULL";
                $sql_data['last_post_mid']          = "NULL";
                $sql_data['last_post_musername']    = "NULL";
                $sql_data['last_post_time']         = "NULL";
            }
        } else {
            $sql_data['last_topic_id']          = "NULL";
            $sql_data['last_topic_subject']     = "NULL";
            $sql_data['last_topic_mid']         = "NULL";
            $sql_data['last_topic_musername']   = "NULL";
            $sql_data['last_topic_time']        = "NULL";

            $sql_data['last_post_id']           = "NULL";
            $sql_data['last_post_topic_id']     = "NULL";
            $sql_data['last_post_mid']          = "NULL";
            $sql_data['last_post_musername']    = "NULL";
            $sql_data['last_post_time']         = "NULL";
        }
        $result = func::db_update(DB_TABLE_CHANNEL, $sql_data, "id=".(int)$channel_id."");
        if (!$result) {
            return false;
        } else {
            return true;
        }
    }
    //////

    // return_trans_group_all
    function return_trans_group_all()
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $trans_group_all    = array();
        $sql = "SELECT trans.id as trans_id,
                    trans.name as trans_name,
                    trans_group.id as trans_group_id,
                    trans_group.name as trans_group_name
                FROM ".DB_TABLE_TRANS_GROUP." trans_group
                    LEFT JOIN ".DB_TABLE_TRANS." trans ON FIND_IN_SET(trans.id, trans_group.trans_id) > 0
                ORDER BY trans_group.rank, trans.rank";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $trans_group_all[$result->fields['trans_group_id']][] = array(
                    'trans_id'                    => $result->fields['trans_id'],
                    'trans_name'                  => $result->fields['trans_name'],
                    'trans_group_id'              => $result->fields['trans_group_id'],
                    'trans_group_name'                  => $result->fields['trans_group_name'],
                );
                $result->MoveNext();
            }
        }
        return true;
    }

    // return_trans_group
    function return_trans_group()
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $trans_group_all    = array();
        $sql = "SELECT * FROM ".DB_TABLE_TRANS_GROUP." ORDER BY rank";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $trans_group[] = array(
                    'id'        => $result->fields['id'],
                    'name'      => $result->fields['name'],
                );
                $result->MoveNext();
            }
        }
        return $trans_group;
    }

    // return_trans_list
    function return_trans_list($channel_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $channel_id = (int)$channel_id;

        $sql = "SELECT trans.id as trans_id,
                    trans.name as trans_name,
                    trans_group.id as trans_group_id,
                    trans_group.name as trans_group_name,
                    channel_trans_group.if_must as channel_trans_group_if_must,
                    channel_trans_group.rank as channel_trans_group_rank
                FROM ".DB_TABLE_CHANNEL_TRANS_GROUP." channel_trans_group
                    LEFT JOIN ".DB_TABLE_TRANS_GROUP." trans_group ON trans_group.id=channel_trans_group.trans_group_id
                    LEFT JOIN ".DB_TABLE_TRANS." trans ON FIND_IN_SET(trans.id, trans_group.trans_id) > 0
                WHERE channel_trans_group.channel_id=".$channel_id."
                ORDER BY channel_trans_group.rank, trans.rank, trans.id";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $trans_list[$result->fields['trans_group_id']]['id']        = $result->fields['trans_group_id'];
                $trans_list[$result->fields['trans_group_id']]['name']      = $result->fields['trans_group_name'];
                $trans_list[$result->fields['trans_group_id']]['if_must']   = $result->fields['channel_trans_group_if_must'];
                $trans_list[$result->fields['trans_group_id']]['rank']      = $result->fields['channel_trans_group_rank'];
                $trans_list[$result->fields['trans_group_id']]['trans'][$result->fields['trans_id']]  = array(
                        'id'                => $result->fields['trans_id'],
                        'name'              => $result->fields['trans_name'],
                );
                $result->MoveNext();
            }
            /*if (!empty($trans_list)) {
                foreach ($trans_list as $key => $row) {
                    $rank[$key] = $row['rank'];
                }
                array_multisort($rank, SORT_NUMERIC, SORT_ASC, $trans_list);
            }*/
        }
        return $trans_list;
    }
    //////

    // return_trans_value
    function return_trans_value($channel_id, $topic_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $channel_id = (int)$channel_id;
        $topic_id   = (int)$topic_id;

        $sql = "SELECT trans.id as trans_id,
                    trans.name as trans_name,
                    trans_group.id as trans_group_id,
                    trans_group.name as trans_group_name,
                    channel_trans_group.if_must as channel_trans_group_if_must,
                    channel_trans_group.rank as channel_trans_group_rank
                FROM ".DB_TABLE_CHANNEL_TRANS_GROUP." channel_trans_group
                    LEFT JOIN ".DB_TABLE_TRANS_GROUP." trans_group ON trans_group.id=channel_trans_group.trans_group_id
                    LEFT JOIN ".DB_TABLE_TOPIC_TRANS." topic_trans ON topic_trans.trans_group_id=channel_trans_group.trans_group_id AND topic_trans.topic_id=".$topic_id."
                    LEFT JOIN ".DB_TABLE_TRANS." trans ON trans.id=topic_trans.trans_id
                WHERE channel_trans_group.channel_id=".$channel_id."
                ORDER BY channel_trans_group.rank";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $trans_value[] = array(
                    'id'            => $result->fields['trans_group_id'],
                    'id'            => $result->fields['trans_group_id'],
                    'name'          => $result->fields['trans_group_name'],
                    'if_must'       => $result->fields['channel_trans_group_if_must'],
                    'rank'          => $result->fields['channel_trans_group_rank'],
                    'trans_id'      => $result->fields['trans_id'],
                    'trans_name'    => $result->fields['trans_name'],
                );
                $result->MoveNext();
            }
        }
        return $trans_value;
    }
    //////

    // return_icon_topic_list
    function return_icon_topic_list()
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $icon_topic_list = array();

        $sql = "SELECT *
                FROM ".DB_TABLE_ICON_TOPIC."
                ORDER BY rank, id";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $icon_topic_list[$result->fields['id']] = array(
                        'id'        => $result->fields['id'],
                        'filename'  => $result->fields['filename'],
                        'rank'      => $result->fields['rank'],
                );
                $result->MoveNext();
            }
        }
        return $icon_topic_list;
    }
    // return_post_quote_path
    function return_post_quote_path($quote_id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $quote_path = array($quote_id);
        $quote_id   = (int)$quote_id;

        $if_end     = false;
        while($if_end === false) {
            $sql = "SELECT id,quote_id,quote_path FROM ".DB_TABLE_POST." WHERE id=".$quote_id."";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
                $if_end = true;
            } else {
                if (!$result->EOF) {
                    if( preg_match("/^[1-9][0-9]{0,18}$/",$result->fields['quote_id']) ){
                        $quote_path[]   = $result->fields['quote_id'];
                        $quote_id       = $result->fields['quote_id'];
                    } else {
                        $if_end = true;
                    }
                }else{
                    $if_end = true;
                }
            }
        }
        return $quote_path;
    }
    //////

    // return_post_attach_list
    function return_post_attach_list($attach_list, $attach_type, $attach_where, $attach_table, $attach_dir)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $str_js_han     = "";
        $result_str     = "";
        $result_js      = "";
        if ($attach_type == "amend") {
            $sql_where  = "post_id=".$attach_where."";
            $attach_url = "&post_id=".$attach_where."";
        } else {
            $sql_where  = "post_id=0 AND member_id=".$attach_where."";
        }
        $filelist       = func::db_select($attach_table, "*", $sql_where, "", "id DESC");
        if (is_array($filelist) && count($filelist) > 0){
            $result_str = "";
            $result_str .= "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\">";
            foreach($filelist as $file){ 
                $path   = $attach_dir."/".$file['dir'];
                if (file_exists($path.$file['filename'])) {
                    list($width, $height, $type, $attr) = @getimagesize($path.$file['filename']);
                }
                if (preg_match("/^(\.gif|\.jpg|\.png)$/i", substr($file['filename'], -4))){
                    $file_icon = "<img src=\"".$CONFIGURE['common']['control_index']."?act=attach".$attach_url."&attach_id=".$file['id']."\" width=\"20\" height=\"20\" border=\"0\" align=\"absmiddle\">";
                } else {
                    $file_icon = "&nbsp;";
                }
                $result_str .= "
                  <tr align=\"center\" bgcolor=\"#DEDEDE\">
                    <td width=\"5%\">".$file_icon."</td>
                    <td width=\"85%\" align=\"left\" title=\"双击修改内容\" ondblclick=\"xajax_set_value_onfocus('brief', '".$file['id']."', $('#brief_".$file['id']."_value').val(), 'text', '255', 'width:500px;text-align:left;');\">
                      <table width=\"100%\" height=\"25\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"content_wrap\"><tr><td class=\"dbclick_modify\" align=\"left\"><div id=\"brief_".$file['id']."\" name=\"brief_".$file['id']."\" class=\"smalltext\"><a href=\"".$CONFIGURE['common']['control_index']."?act=attach".$attach_url."&attach_id=".$file['id']."\" target=\"_blank\">".$file['brief']."</a></div></td></tr></table>
                      <input type=\"hidden\" id=\"brief_".$file['id']."_value\" name=\"brief_".$file['id']."_value\" value=\"".$file['brief']."\">
                    </td>
                    <td width=\"10%\">
                      <span title=\"修改附件描述\"><img src=\"".$SETTING['dir_images']."/icon_edit.gif\" onclick=\"xajax_set_value_onfocus('brief', '".$file['id']."', $('#brief_".$file['id']."_value').val(), 'text', '255', 'width:500px;text-align:left;');\" style=\"cursor:pointer;\" alt=\"描\" /></span>
                      <span title=\"删除附件\"><img src=\"".$SETTING['dir_images']."/icon_delete.gif\" onclick=\"xajax_attach_del('".$attach_type."','".$attach_where."','".$file['id']."','".$attach_list."');\" style=\"cursor:pointer;\" alt=\"删\" /></span>
                    </td>
                  </tr>\n";
            }
            $result_str .= "</table>";
        }
        return array($result_str, $result_js);
    }
    //////

    // return_avatar
    function return_avatar($avatar_type, $avatar_value)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        if ($avatar_value != "") {
            if ($avatar_type == "1" ) {
                $avatar_path = $SETTING['dir_avatar_select']."/".$avatar_value;
            }elseif ($avatar_type == "2" ) {
                $avatar_path = $avatar_value;
            }elseif ($avatar_type == "3" ) {
                $avatar_path = $SETTING['dir_member_attach']."/".$avatar_value;
            }else{
                $avatar_path = "";
            }
        }else{
            $avatar_path = "";
        }
        return $avatar_path;
    }

    // update_area_path
    function update_area_path($id)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $sql = "SELECT id FROM ".DB_TABLE_AREA." ORDER BY id";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $allsub[]   = $result->fields['id'];
                $result->MoveNext();
            }
        }
        foreach ($allsub as $v) {
            $upper_id = $v;
            $path     = array();
            $if_end   = false;
            while($if_end === false) {
                $sql = "SELECT id,upper_id FROM ".DB_TABLE_AREA." WHERE id=".$upper_id."";
                $result = &$db->Execute($sql);
                if (!$result) {
                    echo $db->ErrorMsg();
                    $if_end = true;
                } else {
                    if (!$result->EOF) {
                        $path[]   = $result->fields['id'];
                        $upper_id = $result->fields['upper_id'];
                    }else{
                        $if_end = true;
                    }
                }
            }
            krsort($path);
            $update_sql = "UPDATE ".DB_TABLE_AREA." SET
                               path      = '".addslashes(implode(",", $path))."'
                           WHERE id=".$v."";
            $update_result = $db->Execute($update_sql);
            if (!$update_result) {
                echo $db->ErrorMsg();
            }
        }
        return $allsub;
    }
    //////

    // return_member
    function return_member($id, $field, $cache="0")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $str_query = "";
        if (is_array($field)) {
            foreach ($field as $v) {
                if($v == "*"){
                    $str_query .= $v.",";
                } else {
                    $str_query .= $v." as m_".$v.",";
                }
            }
            $str_query = substr($str_query, 0, -1);
        } else {
            $str_query = $field;
        }
        if (is_array($id)) {
            foreach($id as $k => $v) {
                $where = $k."='".$v."'";
            }
        } else {
            $where = "id='".$id."'";
        }
        $passport_sql = "SELECT ".$str_query." FROM ".DB_TABLE_MEMBER." WHERE ".$where;
        if ( $cache > 0 ) {
            $db->cacheSecs = $cache;
            $passport_result = &$db->CacheExecute($passport_sql);
        } else {
            $passport_result = &$db->Execute($passport_sql);
        }
        if (!$passport_result) {
            echo $db->ErrorMsg();
            $fields = FALSE;
        } else {
            if (!$passport_result->EOF) {
                $fields = $passport_result->fields;
                $total_record = count($fields)/2;
                for ($i=0;$i<$total_record;$i++) {
                    unset($fields[$i]);
                }
            } else {
                $fields = FALSE;
            }
        }
        $passport_result->Close();
        return $fields;
    }

    // check_member_group
    function check_member_group($mid, $time)
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        $result = false;
        if ($time < $this->nowtime) {
            $update_sql = "UPDATE ".DB_TABLE_MEMBER." SET
                               mg_type                  = '".addslashes($CONFIG['default_group_member'])."',
                               mg_type_expired  = '0'
                           WHERE id=".$mid."";
            $update_result = $db->Execute($update_sql);
            if (!$update_result) {
                echo $db->ErrorMsg();
            } else {
                $result = true;
            }
        }
        return $result;
    }

    // return_member_group
    function return_member_group($type, $point, $field, $cache="0")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        if (is_array($field)) {
            $str_query = implode(",", $field);
        } else {
            $str_query = $field;
        }
        $passport_sql = "SELECT ".$str_query." FROM ".DB_TABLE_MEMBER_GROUP."
                         WHERE mg_type='".$type."' AND (mg_point_begin<=".(int)$point." || mg_point_begin IS NULL) AND (mg_point_end>=".(int)$point." || mg_point_end IS NULL)
                         ORDER BY mg_point_begin
                         LIMIT 1";

        if( $cache > 0 ) {
            $db->cacheSecs = $cache;
            $passport_result = &$db->CacheExecute($passport_sql);
        } else {
            $passport_result = &$db->Execute($passport_sql);
        }
        if (!$passport_result) {
            echo $db->ErrorMsg();
            $fields = FALSE;
        } else {
            if (!$passport_result->EOF) {
                $fields = $passport_result->fields;
                $total_record = count($fields)/2;
                for ($i=0;$i<$total_record;$i++) {
                    unset($fields[$i]);
                }
            } else {
                $fields = FALSE;
            }
        }
        $passport_result->Close();
        return $fields;
    }

    // return_member_info
    function return_member_info($id, $field, $cache="0")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        if (is_array($field)) {
            $member_field   = explode(",", $field[0]);
            $member_group   = explode(",", $field[1]);
            foreach ($member_field as $v) {
                if($v == "*"){
                    $str_query_member .= "m.".$v.",";
                } else {
                    $str_query_member .= "m.".$v." as m_".$v.",";
                }
            }
            foreach ($member_group as $v) {
                $str_query_member_group .= "mg.".$v.",";
            }
            $str_query = substr($str_query_member.$str_query_member_group, 0, -1);
            if (is_array($id)){
                foreach($id as $k => $v) {
                    $where = $k."='".$v."'";
                }
            } else {
                $where = "id='".$id."'";
            }
            $passport_sql = "SELECT ".$str_query." FROM ".DB_TABLE_MEMBER." m
                             LEFT JOIN ".DB_TABLE_MEMBER_GROUP." mg ON mg.mg_type=m.mg_type AND mg.mg_point_begin<=m.point AND mg.mg_point_end>=m.point
                             WHERE m.".$where."
                             LIMIT 1";
            if( $cache > 0 ) {
                $db->cacheSecs = $cache;
                $passport_result = &$db->CacheExecute($passport_sql);
            } else {
                $passport_result = &$db->Execute($passport_sql);
            }
            if (!$passport_result) {
                echo $db->ErrorMsg();
                $fields = FALSE;
            } else {
                if (!$passport_result->EOF) {
                    $fields = $passport_result->fields;
                    $total_record = count($fields)/2;
                    for ($i=0;$i<$total_record;$i++) {
                        unset($fields[$i]);
                    }
                } else {
                    $fields = FALSE;
                }
            }
        } else {
            $fields = FALSE;
        }
        return $fields;
    }

    // return_member_list
    function return_member_list($field, $condition="", $orderby="", $limit="", $cache="0")
    {
        global $CONFIG, $CONFIGURE, $SETTING, $ADMIN, $MEMBER, $db;
        if (is_array($field)) {
            $str_query = implode(",", $field);
        } else {
            $str_query = $field;
        }
        $member_list = array();
        $passport_sql = "SELECT ".$str_query." FROM ".DB_TABLE_MEMBER;
        if (!empty($condition)) {
            $passport_sql .= " WHERE ".$condition;
        }
        if (!empty($orderby)) {
            $passport_sql .= " ORDER BY ".$orderby;
        }
        if (!empty($limit)) {
            $passport_sql .= " LIMIT ".$limit;
        }
        if( $cache > 0 ) {
            $db->cacheSecs = $cache;
            $passport_result = &$db->CacheExecute($passport_sql);
        } else {
            $passport_result = &$db->Execute($passport_sql);
        }
        if (!$passport_result) {
            echo $db->ErrorMsg();
            $member_list = FALSE;
        } else {
            while (!$passport_result->EOF) {
                $fields = $passport_result->fields;
                $member_list[$passport_result->fields[0]] = $fields;
                $passport_result->MoveNext();
            }
        }
        $passport_result->Close();
        return $member_list;
    }
}
?>
